if(process.env.NODE_ENV === 'development'){
    console.log('开发环境')
	var BASEURL = "https://www.fastmock.site/mock/9655937256c5377d234110e4279ddfc5/api"
	//var BASEURL = "http://111.229.192.192/v1/admin/connect"
	console.log(BASEURL)
}else{
    console.log('生产环境')
}


const showError = error => {
	let errorMsg = ''
	switch (error.code) {
		case 400:
			errorMsg = '请求参数错误'
			break
		case 401:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = `请求地址出错: ${error.config.url}`
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = error.msg
			break
	}

	uni.showToast({
		title: errorMsg,
		icon: 'none',
		duration: 1000,
		complete: function() {
			setTimeout(function() {
				uni.hideToast();
			}, 1000);
		}
	});
}

const REQUEST = (url,  method = "get", params = {}, callback) => {
	var header = {}
	if(method == "post") {
		header['content-type'] = 'application/x-www-form-urlencoded'
	}
	uni.request({
			url: BASEURL + url,
			method: method,
			header:header,
			data:params,
			success: (response) => {
				uni.hideLoading();
				if(response.data.code == 401) {
					showError(response.data);
					uni.navigateTo({
						url: '/pages/login/index'
					});
				}
				callback(response.data);
			},
			fail: (error) => {
				uni.hideLoading();
				if (error && error.response) {
					showError(error.response);
				}
			},
			complete: () => {
				setTimeout(function() {
					uni.hideLoading();
				}, 250);
			}
		})
}



module.exports = REQUEST